<?php

$habiword=["payment link","vip link","shipping fee","extra payment","extra fee"];

$info=json_decode($this->input->raw_input_stream,true);
$info["sku_price"]=json_decode($info["sku_price"],true);
if($this->goods_model->getbytbid($info["id"],$shop["id"])) die("exist");
$product=array(
    "catid"=>$shop["id"],
    "tbid"=>$info["id"],
    "name"=>$info["name"],
    "thumb"=>serialize($info["img"]),
    "source"=>"aliexpress",
    "source_url"=>"https://www.aliexpress.com/item/xxx/".$info["id"].".html"
);
if(isset($info["sales"]))
    $product["sales"]=intval($info["sales"]);
if(isset($info["reviews"]))
    $product["reviews"]=intval($info["reviews"]);
if(strstr($info["price"],"-")){
    $tmp=explode("-",$info["price"]);
    $product["price"]=trim($tmp[1]);
}else{
    $product["price"]=trim($info["price"]);					
}

       

$product["price"]=str_replace(["US",","],"",$product["price"]);

if($product["price"]>1000)
{
    die("high price");
}
foreach($habiword as $word)
{
    if(strstr($product["name"],$word))
    {
        die("forbidden name");
    }
}
if($product["price"]<3)
{
    die("low price");
}


if(isset($info["catid"]))
    $product["rcatid"]=intval($info["catid"]);
if(isset($info["storeid"]))
    $product["rstoreid"]=intval($info["storeid"]);
if(isset($info["shippingfee"]))
{
    $product["shippingfee"]=$info["shippingfee"];
    $product["price"]+=$product["shippingfee"];
}

$tags="";
$brand="";
$desc="<h2>Product Specifics</h2><p><ul class='props'>";
foreach($info["prop"] as $prop)
{
    $tmp=explode(":",$prop);
    if(in_array(trim($tmp[0]),array('Brand Name')))
    {
        $brand=trim($tmp[1]);
        continue;
    } 
    $desc.="<li>".$prop."</li>";
    if(!in_array(trim($tmp[0]),array('Sale by Pack','Hooded')))
    {
        $tags.=trim($tmp[1]).",";
    }
}
if($tags)
{
    $tags=substr($tags,0,-1);
}

if($brand)
{
    $product["name"]=trim(str_ireplace($brand,"",$product["name"]));
    $product["oname"]=$product["name"];
}
$desc.="</ul></p>";
$product["tags"]=$tags;
$product["desc"]=$desc;


if($info["chart"])
{
    $product["desc"]=$product["desc"].$info["chart"];
}

if(isset($info["meta"]) && isset($info["keyword"]))
{
    $extra=["meta"=>$info["meta"],"keyword"=>$info["keyword"]];
    $product["meta"]=json_encode($extra,JSON_UNESCAPED_UNICODE);
}

$prop=array();
$valmap=[];
$maxprice = 0;
if($info["skuinfo"]){
    
    $total=1;
    $shipcount=0;
    $pricecolindex=0;
    
    foreach($info["skuinfo"] as $onsk=>$vs){
        $total=$total*count($vs["val"]);
        if(strstr($vs["name"],"Ships From")) {
            $shipcount=count($vs["val"]);
            continue;
        }
        $v1=array("name"=>trim(str_replace(":","",$vs["name"])));
        $pricelist=[];
        foreach($vs["val"] as $key=>$vc){
            $v1["value"][]=trim($vc); 
            $valmap[$vs["id"][$key]]=trim($vc);
            if(isset($vs["img"][$key])){
                $v1["image"][]=str_replace("_50x50.jpg","_640x640.jpg",$vs["img"][$key]);
            }                
            foreach($info["sku_price"] as $sku)
            {
                if(isset($vs["id"][$key]) && in_array($vs["id"][$key],explode(",",$sku["skuPropIds"])))
                {
                    if(isset($sku["skuVal"]["actSkuCalPrice"]))
                    {
                        $skuprice=$sku["skuVal"]["actSkuCalPrice"];
                    }
                    else
                    {
                        $skuprice=$sku["skuVal"]["skuCalPrice"];  
                    }
                    $skuprice=str_replace([",","$"],"",$skuprice);
                    if(!in_array($skuprice,$pricelist))
                    {
                        $pricelist[]=$skuprice;
                    }
                    if($skuprice>$maxprice) { $maxprice = $skuprice; }
                    $v1["price"][]=$skuprice;
                    break;
                }
            }
        }				
        if(count($pricelist)>1)
        {
            $pricecolindex=$onsk;
        }		
        $prop["vv"][$onsk]=$v1;
    }
    
    if($pricecolindex>0)
    {
        $newproductprop=["vv"=>[]];
        foreach($prop["vv"] as $k=>$v)
        {
            if($k!=$pricecolindex)
            {
                unset($v["price"]);                
            }
            $newproductprop["vv"][]=$v;
        }
        $prop=$newproductprop;
    }
}

$skuprice_total=[];
if($info["sku_price"])
{
    foreach($info["sku_price"] as $sku)
    {
        if(empty(trim($sku["skuPropIds"]))) continue;
        $tmp_opt=explode(",",$sku["skuPropIds"]);
        if($tmp_opt)
        {
            $opval_list=[];
            foreach($tmp_opt as $_optid)
            {
                if(isset($valmap[$_optid])) $opval_list[]=$valmap[$_optid];
            }            
        }
        if(isset($sku["skuVal"]["actSkuCalPrice"]))
        {
            $skuprice=$sku["skuVal"]["actSkuCalPrice"];
        }
        else
        {
            $skuprice=$sku["skuVal"]["skuCalPrice"];  
        }
        $skuprice=str_replace([",","$"],"",$skuprice);
        if($skuprice>$maxprice) { $maxprice = $skuprice; }
        $skuprice_total[]=["val"=>$opval_list,"price"=>$skuprice];
    }
}
$prop["newsku"]=$skuprice_total;
if($maxprice>0 && $product["price"]>$maxprice)
{
    $product["price"]=$maxprice;
}

$product["option"]=serialize($prop);
$newproduct=$this->goods_model->create($product);
if($newproduct){
    die("ok");
}else{
    die("error");
}		
